//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ForwardValueQuote.h"
using namespace Cephei::QL::Quotes;
#include <gen/QL/Index.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (Cephei::QL::IIndex^ index, DateTime fixingDate) : CQuote(CForwardValueQuote::typeid)
{
    CIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phForwardValueQuote = NULL;
#endif
        _Cindex = safe_cast<CIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::Index>& _index = static_cast<boost::shared_ptr<QuantLib::Index>&> (_Cindex->GetShared ()); 
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate); //d
        _ppForwardValueQuote = new boost::shared_ptr<QuantLib::ForwardValueQuote> (new QuantLib::ForwardValueQuote ( _index,  _fixingDate ));
        SetQuote (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardValueQuote));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (boost::shared_ptr<QuantLib::ForwardValueQuote>& childNative, Object^ owner) : CQuote(CForwardValueQuote::typeid)
{
#ifdef HANDLE
	_phForwardValueQuote = NULL;
#endif
	_ppForwardValueQuote = &childNative;
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardValueQuote));
}
Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (QuantLib::ForwardValueQuote& childNative, Object^ owner) : CQuote(CForwardValueQuote::typeid)
{
#ifdef HANDLE
	_phForwardValueQuote = NULL;
#endif
	_ppForwardValueQuote = new boost::shared_ptr<QuantLib::ForwardValueQuote> (&childNative);
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardValueQuote));
    _ForwardValueQuoteOwner = owner;
    _QuoteOwner = owner;
}

Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (CForwardValueQuote^ copy) : CQuote(CForwardValueQuote::typeid)
{
#ifdef HANDLE
	_phForwardValueQuote = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppForwardValueQuote = new boost::shared_ptr<QuantLib::ForwardValueQuote> (copy->GetShared());
        _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardValueQuote));
    }
}
Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (PLATFORM::Type^ t) : CQuote(CForwardValueQuote::typeid)
{
#ifdef HANDLE
	_phForwardValueQuote = NULL;
#endif
	if (!t->IsSubclassOf(CForwardValueQuote::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (QuantLib::Handle<QuantLib::ForwardValueQuote>& childNative, Object^ owner)  : CQuote(CForwardValueQuote::typeid)
{
	_phForwardValueQuote = &childNative;
	_ppForwardValueQuote = &static_cast<boost::shared_ptr<QuantLib::ForwardValueQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardValueQuote));
    _ForwardValueQuoteOwner = owner;
}
Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (QuantLib::Handle<QuantLib::ForwardValueQuote> childNative)  : CQuote(CForwardValueQuote::typeid)
{
	_phForwardValueQuote = &childNative;
	_ppForwardValueQuote = &static_cast<boost::shared_ptr<QuantLib::ForwardValueQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardValueQuote));
}
#endif
#ifdef STRUCT
Cephei::QL::Quotes::CForwardValueQuote::CForwardValueQuote (QuantLib::ForwardValueQuote childNative)  : CQuote(CForwardValueQuote::typeid)
{
#ifdef HANDLE
	_phForwardValueQuote = NULL;
#endif
	_ppForwardValueQuote = new boost::shared_ptr<QuantLib::ForwardValueQuote> (new QuantLib::ForwardValueQuote (childNative));
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardValueQuote));
}
#endif

Cephei::QL::Quotes::CForwardValueQuote::~CForwardValueQuote ()
{
    if (_ppForwardValueQuote != NULL)
    {
	    delete _ppForwardValueQuote;
        _ppForwardValueQuote = NULL;
    }
}
Cephei::QL::Quotes::CForwardValueQuote::!CForwardValueQuote ()
{
    if (_ppForwardValueQuote != NULL)
    {
	    delete _ppForwardValueQuote;
    }
}
QuantLib::ForwardValueQuote& Cephei::QL::Quotes::CForwardValueQuote::GetReference ()
{
    if (_ppForwardValueQuote == NULL) throw REFNEW NativeNullException ();
	return **_ppForwardValueQuote;
}
boost::shared_ptr<QuantLib::ForwardValueQuote>& Cephei::QL::Quotes::CForwardValueQuote::GetShared ()
{
    if (_ppForwardValueQuote == NULL) throw REFNEW NativeNullException ();
	return *_ppForwardValueQuote;
}
QuantLib::ForwardValueQuote* Cephei::QL::Quotes::CForwardValueQuote::GetPointer ()
{
    if (_ppForwardValueQuote == NULL) throw REFNEW NativeNullException ();
	return &**_ppForwardValueQuote;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ForwardValueQuote>& Cephei::QL::Quotes::CForwardValueQuote::GetHandle ()
{
	if (_phForwardValueQuote == NULL)
	{
		_phForwardValueQuote = new Handle<QuantLib::ForwardValueQuote> (*_ppForwardValueQuote);
	}
	return *_phForwardValueQuote;
}
#endif
bool Cephei::QL::Quotes::CForwardValueQuote::HasNative () 
{
	return (_ppForwardValueQuote != NULL);
}

Boolean Cephei::QL::Quotes::CForwardValueQuote::IsValid::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppForwardValueQuote)->isValid ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Quotes::IForwardValueQuote^ Cephei::QL::Quotes::CForwardValueQuote::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppForwardValueQuote)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Quotes::CForwardValueQuote::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppForwardValueQuote)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Quotes::IForwardValueQuote^ Cephei::QL::Quotes::CForwardValueQuote_Factory::Create (Cephei::QL::IIndex^ index, DateTime fixingDate)
{
    return REFNEW CForwardValueQuote ( index,  fixingDate);
}
